package com.evon.yardmanagement.service;

import java.util.List;



import com.evon.yardmanagement.dao.ActionDAO;
import com.evon.yardmanagement.dao.RoleDAO;
import com.evon.yardmanagement.model.Action;
import com.evon.yardmanagement.model.Role;
import com.evon.yardmanagement.security.YardSecurityManager;
import com.evon.yardmanagement.transformer.BaseTransformer;
import com.evon.yardmanagement.transformer.RoleTransformer;
import com.evon.yardmanagement.ui.dto.ActionDTO;
import com.evon.yardmanagement.ui.dto.RoleDTO;

/**
 * Class to provide the role management service.
 * 
 * @author vgaurav
 * 
 */
public class RoleManagementService {

	private static RoleDAO dao = new RoleDAO();

	public static List<RoleDTO> getAllRoles(int vendorId) {
		List<Role> roles = dao.findByProperty("owningCompany.id", vendorId);
		return RoleTransformer.transformToDTO(roles);
	}

	public static RoleDTO getRole(int roleId) {
		Role role = dao.getRole(roleId);
		return RoleTransformer.transformToDTO(role);
	}

	public static List<ActionDTO> getAllActions() {
		List<Action> actions = new ActionDAO().findAll();
		return new BaseTransformer<Action, ActionDTO>(Action.class, ActionDTO.class)
				.transformToDTO(actions);
	}

	public static void save(RoleDTO roledto) {
		Role role ;
		if(roledto.getId() != 0){
			//first load the object if present in the session
			 role = (Role)dao.getSession().load(Role.class, roledto.getId());
			 role =  new BaseTransformer<Role, RoleDTO>(Role.class, RoleDTO.class).transformToModel(roledto,role);
		}else {
			role = new BaseTransformer<Role, RoleDTO>(Role.class, RoleDTO.class).transformToModel(roledto);
		}
		role.setOwningCompany(YardSecurityManager.getCurrentVendor());
		dao.saveOrUpdate(role);
	}

}
